<template>
  <div class="mobile-register">
    <div class="register-header">
      <el-button 
        type="text" 
        @click="$router.go(-1)"
        class="back-btn"
      >
        <el-icon><ArrowLeft /></el-icon>
        返回
      </el-button>
    </div>

    <div class="register-content">
      <div class="register-logo">
        <el-icon size="60"><UserFilled /></el-icon>
        <h1>用户注册</h1>
        <p>创建您的新账户</p>
      </div>

      <el-form 
        ref="registerFormRef"
        :model="registerForm" 
        :rules="registerRules"
        class="register-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item prop="email">
          <el-input
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            :prefix-icon="Message"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请确认密码"
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button 
            type="primary" 
            size="large"
            class="register-btn"
            :loading="loading"
            @click="handleRegister"
          >
            {{ loading ? '注册中...' : '注册' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="register-footer">
        <el-divider>
          <span class="divider-text">已有账户？</span>
        </el-divider>
        <el-button 
          type="text" 
          size="large"
          @click="$router.push('/login')"
          class="login-link"
        >
          立即登录
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { ArrowLeft, UserFilled, User, Message, Lock } from '@element-plus/icons-vue'

const registerFormRef = ref<FormInstance>()
const loading = ref(false)

// 表单数据
const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

// 确认密码验证
const validateConfirmPassword = (rule: any, value: any, callback: any) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

// 表单验证规则
const registerRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在3到20个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

// 处理注册
async function handleRegister() {
  if (!registerFormRef.value) return
  
  try {
    const valid = await registerFormRef.value.validate()
    if (!valid) return

    loading.value = true
    
    // 模拟注册请求
    await new Promise(resolve => setTimeout(resolve, 2000))
    
    ElMessage.success('注册成功！')
    // 这里可以添加实际的注册逻辑
    
  } catch (error) {
    ElMessage.error('注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.mobile-register {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.register-header {
  margin-bottom: 20px;
}

.back-btn {
  color: white !important;
  font-size: 16px;
}

.register-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  max-width: 400px;
  margin: 0 auto;
  width: 100%;
}

.register-logo {
  text-align: center;
  margin-bottom: 40px;
  color: white;
}

.register-logo .el-icon {
  margin-bottom: 16px;
}

.register-logo h1 {
  font-size: 1.8rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.register-logo p {
  opacity: 0.9;
  font-size: 1rem;
}

.register-form {
  background: white;
  padding: 30px 20px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.register-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
}

.register-footer {
  margin-top: 20px;
  text-align: center;
}

.divider-text {
  color: white;
  opacity: 0.8;
  font-size: 14px;
}

.login-link {
  color: white !important;
  font-size: 16px;
  font-weight: 600;
}

:deep(.el-divider__text) {
  background: transparent;
}

:deep(.el-divider--horizontal) {
  border-color: rgba(255, 255, 255, 0.3);
}
</style>
